package com.wuji1626.spark.rdd.serial

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object RDD_Serial {

  def main(args: Array[String]): Unit = {

    // Step1: 准备环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)
    // Step2: Serial
    val rdd:RDD[String] = sc.makeRDD(Array("Hello World", "Hello Spark", "Hello Scala"))

    val search = new Search("Hello")
    search.getMatch3(rdd).collect().foreach(println)

    // Step3: 关闭环境
    sc.stop()
  }
  // 类的构造参数是类的属性，构造参数需要进行闭包检测，因此要使用该属性，其实等同于类进行闭包检测
  class Search(query:String){
    def isMatch(s: String): Boolean = {
      s.contains(query)
    }
    // 函数序列化案例
    def getMatch1 (rdd: RDD[String]): RDD[String] = {
      rdd.filter(isMatch)
    }
    // 属性序列化案例
    def getMatch2(rdd: RDD[String]): RDD[String] = {
      rdd.filter(x => x.contains(query))
    }
    def getMatch3(rdd: RDD[String]): RDD[String] = {
      var queryStr = query
      rdd.filter(x => x.contains(queryStr))
    }
  }
}
